var msg = {
		init : function() {
			msg.msgGrid = $('#msgGrid');
			msg.menu = $('#menu');
			msg.msgDetailDialog = $('#msgDetailDialog');
			msg.code = $('#code');
			msg.age = $('#age');
			msg.msgDetailForm = $('#msgDetailForm');
			msg.msgAddMsg = $('#msgAddMsg');
			
			msg.msgAddForm = $('#msgAddForm');
			msg.msgUpdateForm = $('#msgUpdateForm');
			
			//新增时候的字段
			msg.title = $('#title');
			msg.content = $('#content');
			msg.expireTime = $('#expireTime');
			msg.state = $('#state');
			msg.receiveMan = $('.receiveMan');
			
			msg.menuUpdate = $('#menuUpdate');
			msg.msgUpdateDialog = $('#msgUpdateDialog');
			
			function formatOper(val,row,index){  
			    return '<a href="#" onclick=""></a>';  
			} 
			msg.msgGrid.datagrid({
			    url:window.ctx + '/AssistMsgServlet?command=listMsg&code='+code,
			    fixed: true,
			    fitColumns: true,
			    striped: true,
			    rownumbers: true,
			    pagination: true,
			    pageSize: 15,
			    pageList: [10, 15, 20, 40, 50],
			    pageNumber: 1,
			    toolbar: '#msgToolbar',
			    columns:[[
					{field:'id', width:100, checkbox: true}, 
					{field:'title', title:'标题', width:60, align:'center'},
					{field:'content', title:'内容', width:100, align:'center'}, 
					{field:'sysUser__name', title:'发送者', width:50, align:'center'},
					
					{field:'sysUser__code', hidden:true,title:'发送者账号', width:60, align:'center'},
					
					{field:'sysRole__name', title:'发送角色', width:60, align:'center'},
					{field:'sendTime', title:'发送时间', width:100, align:'center',sortable: true },
					{field:'expireTime', title:'过期时间', width:100, align:'center',sortable: true },
					{field:'state', title:'状态', width:40, align:'center'},
					{field:'operate', title: '操作', width:100, formatter: msg.row.crud, align:'center'},
			    ]],
			    onClickRow: function (rowIndex, rowData) {
                    $(this).datagrid('unselectRow', rowIndex);
                },
                
				onDblClickRow: function(rowIndex, rowData) {
					/* msg.toolbar.showUpdateDialog(rowData.id); */
				},
				
				onLoadSuccess:function(data){  
					//空白占位
					var $empty = $('.empty');
					$empty.linkbutton({text:'',plain:true,disabled:true,iconCls:'icon-empty'});
					//详情
					 var $detail = $('.detailcls');
					 $detail.linkbutton({text:'',plain:true,iconCls:'icon-more'});
					 $detail.tooltip({
						    position: 'top',
						    content: '<span style="color:#fff">详情</span>',
						    onShow: function(){
								$(this).tooltip('tip').css({
									backgroundColor: '#666',
									borderColor: '#666'
								});
						    }
						});
					//生效
					var effect = $('.editcls');
					effect.linkbutton({text:'',plain:true,iconCls:'icon-eft'});
					effect.tooltip({
						    position: 'top',
						    content: '<span style="color:#fff">生效</span>',
						    onShow: function(){
								$(this).tooltip('tip').css({
									backgroundColor: '#666',
									borderColor: '#666'
								});
						    }
						});
					//删除
					var delet = $('.deletecls');
					delet.linkbutton({text:'',plain:true,iconCls:'icon-remove'});
					delet.tooltip({
					    position: 'top',
					    content: '<span style="color:#fff">删除</span>',
					    onShow: function(){
							$(this).tooltip('tip').css({
								backgroundColor: '#666',
								borderColor: '#666'
							});
					    }
					});
				},
				onBeforeLoad: function (param) {
					var msg=$('#msg').val();
					param.msg=msg;
			    },
			});
		},
		toolbar: {
			showCombotree: function() {
				msg.menu.tree({
					url: window.ctx + '/msgServlet?command=listMenu',
					lines: true,
					multiple: true,
					checkbox: true,
					onlyLeafCheck: true,
					onLoadSuccess: function(node, data) {
						$(this).tree('expandAll')
					}
				});
			},
			showAddDialog: function() {
				msg.msgAddMsg.dialog('open');
				msg.code.focus();
				msg.age.numberspinner('setValue', 20);
			},
			 resetAddForm: function() {
				msg.msgAddForm.form('reset');
			}, 
		},
		
		btn : {
			sureAddMsg : function() {
				/* alert($('#receiveMan').combobox("getValue")) */
				if((msg.title.val() !='') && (msg.content.val() !='')  ){
					$.post(window.ctx + '/AssistMsgServlet?command=addMsg',msg.msgAddForm.serialize(), function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '新增成功！',
								timeout: 3000,
								showType: 'slide'
							});
							msg.msgAddMsg.dialog('close');
							msg.msgGrid.datagrid('reload');
							msg.toolbar.resetAddForm();
						} else {
							$.messager.alert('系统提示', '新增失败！', 'error')
						}
						$.messager.progress('close');
					});
				}else{
					msgShow('系统提示', '输入项不能为空！', 'warning');
					return false;
				}
			},
			updateMsg : function() {
				$('.msgUpdateFormTitle').val($('#msgDetailFormTitle').val());
				$('.msgUpdateFormContent').textbox("setValue", $("#msgDetailFormContent").textbox('getValue'));
				$('.msgUpdateFormExpireTime').datetimebox('setValue',($('#msgDetailFormExpireTime').datetimebox('getValue')));
				$('.msgUpdateFormReceiveClazz').val($('.msgDetailFormReceiveClazz').val());
				$('.msgUpdateFormState').val($('#msgDetailFormState').val());
				
				msg.msgUpdateDialog.dialog('open');
			},
			
			submitUpdateMsg:function(){
				var updateId = $('#msgDetailFormId').val();
				
				$.post(window.ctx + '/AssistMsgServlet?command=updateMsg&id='+updateId,msg.msgUpdateForm.serialize(), function(data) {
					if (data == 1) {
						$.messager.show({
							title: '系统提示',
							msg: '修改成功！',
							timeout: 3000,
							showType: 'slide'
						});
						msg.msgUpdateDialog.dialog('close');
						$('#msgDetailDialog').dialog('close');
						msg.msgUpdateForm.form('reset');
						msg.msgGrid.datagrid('reload');
						msg.toolbar.resetAddForm();
					} else {
						$.messager.alert('系统提示', '修改失败！', 'error')
					}
					$.messager.progress('close');
				});
			},
			msgSerach: function(){
				msg.msgGrid.datagrid('load',{
					msgSerachTitle: $('#msgSerachTitle').val(),
					msgSerachStyle: $('#msgSerachStyle').combobox("getValue"),
					msgSerachRole: $('#msgSerachRole').combobox("getValue"),
					msgSerachState: $('#msgSerachState').combobox("getValue"),
					msgSerachTime: $('#msgSerachTime').combobox("getValue")
				});
				$('#msgSerachTime').combobox('reload');
			},
			
			hiddenUpdateDialog: function(){
				msg.msgUpdateDialog.dialog('close');
			},
			
			hiddenDetailDialog: function() {
				$('#msgDetailDialog').dialog('close');
			},
			hiddenAddMsgDialog: function() {
				msg.msgAddForm.form('clear');
				msg.toolbar.resetAddForm();
				msg.msgAddMsg.dialog('close');
			},
		},
		
		row : {
			crud: function(value, row, index) {
				var id = row.id;
				var state = row.state;
				var operations = [];
				
				var epy = '<span class="empty"></span>';
				var det = '<a class="detailcls" href="javascript:void(0)" onclick="msg.row.detailMsg(\'' + id + '\',  \'' + state + '\',  \'' + index + '\')">' + value + '</a>';
				var edt = '<a class="editcls" href="javascript:void(0)" onclick="msg.row.effectMsg(\'' + id + '\',  \'' + state + '\',  \'' + index + '\')">' + value + '</a>';
				var del = '<a class="deletecls" href="javascript:void(0)" onclick="msg.row.deleteMsg(\'' + id + '\',  \'' + state + '\',  \'' + index + '\')">' + value + '</a>';
				if(row.sysUser__code == code && row.sysRole__name != '学员'){
					if (state=='启用') {
						operations.push(det);
						operations.push(epy);
						operations.push(del);
					}else if(state=='待启用'){
						operations.push(det);
						operations.push(edt);
						operations.push(del);
					}else {
						operations.push(det);
						operations.push(epy);
						operations.push(epy);
					}
				}else {
					operations.push(det);
					operations.push(epy);
					operations.push(epy);
				}
				return operations.join('');
			},
			
			detailMsg: function(id) {
				 $.post(window.ctx + '/AssistMsgServlet?command=detailMsg&id='+id, function(data) {
					if (data){
						data = $.parseJSON(data);
						msg.msgDetailForm.form('load', {
							id: data[1].id,
							title: data[1].title,
							content: data[1].content,
							sendMan: data[1].sysUser__name,
							sendRole: data[1].sysRole__name,
							receiveClazz: data[0].sysRole__name,
							sendTime: data[1].sendTime,
							expireTime: data[1].expireTime,
							state: data[1].state,
						});
						
						if(data[1].sysUser__code == code && data[1].sysRole__name != '学员'){
							$('#updateBtn').linkbutton('enable');
						}else{
							$('#updateBtn').linkbutton('disable');
						}
						
						$('.msgUpdateFormReceiveMan').combobox({
							url:window.ctx + '/AssistMsgServlet?command=listPerson',
							method:'post',
							valueField:'name',
							textField:'name',
							panelHeight:'auto',
							multiple:true,
							editable:false,
							required:true,
							onLoadSuccess: function () { 
				                var val = $(this).combobox("getData"); 
				                var valStr = $('.msgDetailFormReceiveClazz').combobox("getData");
				               /*  console.info(valStr) */
				                /* for (var i = 0;i < val.length;i++ ) { 
				                	for(var j = 0;j < valStr.length;j++){
				                		if(val[i].name == val[j].name){
				                			 $(this).combobox("select",val[i].name); 
				                		}
				                	}
				                }  */
				            },
						
						});
						
						
						$('.msgDetailFormReceiveClazz').combobox({
							url:window.ctx + '/AssistMsgServlet?command=detailPersonMsg&id='+id,
							method:'post',
							valueField:'name',
							textField:'name',
							panelHeight:'auto',
							editable:false,
							required:true,
							onLoadSuccess: function () { 
								
				                /*var val = $(this).combobox("getData"); 
				                var valStr = $('.msgUpdateFormReceiveMan').combobox("getData");
				                for (var i = 0;i < val.length;i++ ) { 
				                	for(var j = 0;j < valStr.length;j++){
				                		if(val[i].name == valStr[j].name){
				                			$('.msgUpdateFormReceiveMan').combobox("select",valStr[j].name); 
				                		}
				                	}
				                } */
				            },
				            /*onUnselect: function(){
				            	$('.msgDetailFormReceiveClazz').combobox('reload'); 
				            }*/
						});
						
						$.each(data[0], function(index, content){
							$('.msgDetailFormReceiveClazz').combobox('setValues', '点击查看');
						})
						/* if(data.sysRole__name=='管理员'||data.sysRole__name=='教师'){
							$('#clazzP').css("display", "none");
						}else if(data.sysRole__name=='学员'){
							$('#clazzP').css("display", "block");
						} */
						
						$('#msgDetailDialog').dialog('open');
						$('.detailcls').tooltip('hide');
					} else {
						$.messager.progress('close');
					}
				}); 
			},
			effectMsg: function(id, state, index) {
				$.post(window.ctx + '/AssistMsgServlet?command=effectMsg&id='+id+'&state='+state, function(data) {
					if (data){
						$.messager.show({
							title: '系统提示',
							msg: '状态更改成功！',
							timeout: 3000,
							showType: 'slide'
						});
						$('.editcls').tooltip('hide');
						msg.msgGrid.datagrid('reload');
					} else {
						$.messager.progress('close');
					}
				});
			},
			deleteMsg: function(id, state, index) {
				$.messager.confirm('删除', '站内消息已建立，是否确定删除？', function(result){
					if (result){
						$.post(window.ctx + '/AssistMsgServlet?command=deleteMsg&id='+id+'&state='+state, function(data) {
							$.messager.show({
								title: '系统提示',
								msg: '信息删除成功！',
								timeout: 3000,
								showType: 'slide'
							});
						});
						msg.msgGrid.datagrid('clearSelections');
						msg.msgGrid.datagrid('reload');
					} else {
						$.messager.progress('close');
					}
				});
			}
		}
	};
	
	$(function(){
		msg.init();
	});
